package org.example.thread;

import org.example.pool.MyRunnable;

import java.util.concurrent.CountDownLatch;

public class CalCount {
    public  static int count=0;
    public static CountDownLatch cdl = new CountDownLatch(2);
    public static void main(String[] args) throws InterruptedException {
        Runnable r = new MyThread();
        Thread t1 = new Thread(r);
        Thread t2 = new Thread(r);
        t1.start();
        t2.start();
        //让主线程阻塞
        cdl.await();
        System.out.println(CalCount.count);
    }

  /*  public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 100000; i++) {
                synchronized (CalCount.class) {
                    CalCount.count++;
                }
            }
            System.out.println(Thread.currentThread().getName()+"已经运行完毕");
        });
        thread.start();
        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 100000; i++) {
                synchronized (CalCount.class) {
                    CalCount.count++;
                }
            }
            System.out.println(Thread.currentThread().getName()+"已经运行完毕");
        });
        thread2.start();
        Thread thread3 = new Thread(() -> {
            for (int i = 0; i < 100000; i++) {
                synchronized (CalCount.class) {
                    CalCount.count++;
                }
            }
            System.out.println(Thread.currentThread().getName()+"已经运行完毕");
        });
        thread3.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(CalCount.count);
    }*/

    public static synchronized void calCount(){
        for(int i=1;i<=100000;i++){
            CalCount.count++;
        }
        CalCount.cdl.countDown();
    }
}
